<!--
 * @Descripttion: 订单管理
 * @version:
 * @Author: 赵致喜
 * @Date: 2020-10-12 18:48:41
 * @LastEditors: 穆宏宇
 * @LastEditTime: 2020-11-26 19:07:10
-->
<template>
	<div class="bigDiv">
		<el-form label-position="'right'" label-width="120px" label-suffix=":">
			<div class="topNav">
				<el-button @click="refreshControlHandle" type="text" style="float: right;;margin-left: 10px;">重置</el-button>
				<el-button @click="searchControlHandle" type="text" style="float: right;">查询</el-button>
				<el-button type="text">
					<el-upload :action="fileUrl" :headers="fileHeaders" :show-file-list="false" :file-list="fileList"
						:on-success="handleAvatarSuccess" :on-error="handleAvatarError" multiple :limit="10000">
						<div class="el-upload__text">导入进出项票</div>
					</el-upload>
				</el-button>
				<el-button @click="exportOrder" type="text">导入模板下载</el-button>
			</div>
			<el-col :span="6">
				<el-form-item label="订单号">
					<el-input type="text" v-model="searchFormData.orderNo" />
				</el-form-item>
			</el-col>
			<el-col :span="6">
				<el-form-item label="sku">
					<el-input type="text" v-model="searchFormData.sku" />
				</el-form-item>
			</el-col>
		</el-form>
		<div class="table-box">
			<el-table :data="tableData" border @selection-change="handleSelectionChange" height="calc(100vh - 300px)">
				<el-table-column type="selection" width="50" fixed="left"></el-table-column>
				<el-table-column label="序号" width="50" fixed="left">
					<template slot-scope="scope">
						{{scope.$index+1}}
					</template>
				</el-table-column>
				<el-table-column prop="orderNo" label="订单号" fixed="left">
				</el-table-column>
				<el-table-column prop="sku" label="sku" fixed="left">
				</el-table-column>
				<el-table-column prop="invoiceIn" label="进项票标识" fixed="left">
					<template slot-scope="scope">
						{{scope.row.invoiceIn == 0?'未进':''}}
						{{scope.row.invoiceIn == 1?'已进':''}}
					</template>
				</el-table-column>
				<el-table-column prop="invoiceInDate" label="进项票插入时间">
				</el-table-column>
				<el-table-column prop="invoiceOut" label="出项票标识">
					<template slot-scope="scope">
						{{scope.row.invoiceOut == 0?'未出':''}}
						{{scope.row.invoiceOut == 1?'已出':''}}
					</template>
				</el-table-column>
				<el-table-column prop="invoiceOutDate" label="出项票插入时间">
				</el-table-column>
				<el-table-column label="操作" fixed="right" width="200">
					<template slot-scope="scope">
						<el-button @click="changePaper(scope.row,1)" size="mini" v-if="scope.row.invoiceIn == 0">进票确认
						</el-button>
						<el-button @click="changePaper(scope.row,0)" size="mini" v-else-if="scope.row.invoiceIn == 1">
							进票取消</el-button>
						<el-button @click="changePaper2(scope.row,1)" size="mini" v-if="scope.row.invoiceOut == 0">出票确认
						</el-button>
						<el-button @click="changePaper2(scope.row,0)" size="mini" v-else-if="scope.row.invoiceOut == 1">
							出票取消</el-button>
					</template>
				</el-table-column>
			</el-table>

			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-size="pageData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageData.total"
				style="text-align: right;">
			</el-pagination>

		</div>



		<el-dialog title="批量对账" :visible.sync="remarkDialogShow" width="500px">
			<el-input type="textarea" placeholder="请输入备注" v-model="remark" maxlength="1000" show-word-limit></el-input>
			<div slot="footer" class="dialog-footer">
				<el-button @click="remarkDialogShow = false">取 消</el-button>
				<el-button type="primary" @click="submitHandle(remark)">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog title="开票信息信息" :visible.sync="paperDialogShow" width="500px">
			<el-form label-position="'right'" label-width="120px" label-suffix=":">
				<el-form-item label="发票类型">
					<el-select v-model="paperDict.receiptType">
						<el-option value="0" label="增值税普通发票"></el-option>
						<el-option value="1" label="增值税专用发票"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="税号">
					<el-input v-model="paperDict.dutyParagraph" />
				</el-form-item>
				<el-form-item label="单位名称">
					<el-input v-model="paperDict.unitName" />
				</el-form-item>
				<el-form-item label="发票打印电话">
					<el-input v-model="paperDict.registrationTelephone" />
				</el-form-item>
				<el-form-item label="开户银行">
					<el-input v-model="paperDict.depositBank" />
				</el-form-item>
				<el-form-item label="银行账号">
					<el-input v-model="paperDict.accountNumber" />
				</el-form-item>
				<el-form-item label="发票打印地址">
					<el-input v-model="paperDict.registeredAddress" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitHandle2()">保存</el-button>
			</div>
		</el-dialog>

	</div>

	</div>
</template>

<script>
	import fileUpdata from "@/api/file.config";
	import {
		findTOrderManagerList,
		setReceivingStatus,
		findOrgUserList,
		getUser,
		findOrgByMangerList,
		getTreceipt,
		getOrgLazyTree,
		getOrgLazyTree2,
		getOrgChAll,
		getOrgLazyTree3,
		managerReconciliationExport,
		selReconciliationInvoiceOrder,
		exportInvoiceOrder,
		ImportPurchaseOrder,
		upFinanceInvoice,
		selFinanceInvoice,
		selFinanceInvoiceHeader,
		downloadInvoiceDistribute,
		selInvoiceHistory,
		upInvoiceHistoryIn,
		upInvoiceHistoryOut,
		downloadInvoiceHistory
	} from '@/api/admin.api'
	import moment from 'moment'
	export default {
		name: 'supplierReconciliation',
		data() {
			return {
				searchFormData: {
					orderNo: "",
					sku: ""
				},
				tableData: [],
				// 分页数据
				pageData: {
					// 当前分页
					pageNum: 1,
					// 分页条数
					pageSize: 10,
					// 数据总数
					total: 0,
				},
				// 表格选中数据
				tableSelectData: [],
				remarkDialogShow: false,
				paperDialogShow: false,
				remark: "",
				people: {
					account: "",
					name: "",
					org: "",
					sjorg: ""
				},
				peopleList: [],
				orgList: [],
				paperDict: {},
				treeData: [],
				// 树配置项
				defaultProps: {
					children: 'children',
					label: 'name',
					isLeaf: 'leaf'
				},
				multipleSelection: [],
				fileHeaders: fileUpdata.headers(),
				fileUrl: "/vapi/reconciliation/reconciliationInvoice/importInvoiceHistory",
				fileList: [],
				list: [],
				fileHeaders2: fileUpdata.headers(),
				fileUrl2: "/vapi/file/batchUploadFile",
				fileList2: [],
				list2: [],
				headerArr: []
			}
		},
		methods: {
			changePaper(row, t) {
				upInvoiceHistoryIn([{
					id: row.id,
					invoiceIn: t
				}]).then((res) => {
					this.getTableData();
					this.$message({
						type: 'success',
						message: '修改成功!'
					});
				})
			},
			changePaper2(row, t) {
				upInvoiceHistoryOut([{
					id: row.id,
					invoiceOut: t
				}]).then((res) => {
					this.getTableData();
					this.$message({
						type: 'success',
						message: '修改成功!'
					});
				})
			},
			selInvoiceHeader() {
				selFinanceInvoiceHeader().then((res) => {
					this.headerArr = res;
				})
			},
			submitHandle2() {
				upFinanceInvoice(this.paperDict).then((res) => {
					if (res.code == 200) {
						this.getTableData();
						this.$message({
							type: 'success',
							message: '修改成功!'
						});
						this.paperDialogShow = false;
					}
				})
			},
			// 上传成功
			handleAvatarSuccess(res) {
				if (res && res.indexOf("成功")>-1) {
					this.$message({
						message: '导入成功',
						type: 'success'
					})
					this.fileList = []
					this.getTableData()
				}
			},
			//  上传失败
			handleAvatarError(err) {
				this.$message({
					message: '上传失败',
					type: 'warning'
				})
			},
			handleAvatarSuccess2(res) {
				if (res && res.code === 200) {
					this.$message({
						message: '上传成功',
						type: 'success'
					})
					this.fileList = []
					this.getTableData()
				}
			},
			//  上传失败
			handleAvatarError2(err) {
				this.$message({
					message: '上传失败',
					type: 'warning'
				})
			},
			clear() {
				var str = this.searchFormData.orgName;
				if (str.length == 0) {
					this.treeData = [];
					this.searchFormData.orgId = '';
					this.searchFormData.orgName = '';
				}
			},
			enterSearch: function(e) {
				if (e.keyCode === 13) {
					console.log(11);
					console.log(e.target.value);

					getOrgLazyTree3({
						id: e.target.value
					}).then((data) => {
						console.log(data);
						this.treeData = data;
						this.$refs.selectReport.blur();
						var that = this;
						setTimeout(function() {
							that.$refs.selectReport.focus();
						}, 300);
					})
				}
			},
			dataFilter(val) {
				console.log(val);
			},
			changeType(e) {
				this.searchFormData.type = e;
			},
			// 左侧树点击
			changeTree(item) {
				var that = this;
				that.searchFormData.orgName = item.name;
				that.searchFormData.orgId = item.id;
				that.searchFormData.allCh = item.allCh;
				that.$refs.selectReport.blur();
			},
			getOrgLazyTree() {
				var that = this;
				findOrgByMangerList().then((data) => {
					console.log(data);
					that.treeData = data;
				})
			},
			// 组织机构节点数据
			loadNode(node, resolve) {
				if (node.level === 0) {
					// findOrgByMangerList().then((res) => {
					// 	res.forEach(item => {
					// 		// 检查是否有叶子节点
					// 		item['leaf'] = false
					// 	})
					// 	if (res.length > 0) {
					// 		this.form = res[0];
					// 	}
					// 	return resolve(res)
					// })
				} else if (node.data.allCh) {
					getOrgLazyTree({
						id: node.data.id,
						type: node.data.type
					}).then((res) => {
						res.forEach(item => {
							// 检查是否有叶子节点
							item['leaf'] = !item.allCh
						})
						return resolve(res)
					})
				} else {
					return resolve([])
				}
			},
			getReturnNode(node, _array, value) {
				let isPass = node && node.label && node.label.indexOf(value) !== -1
				isPass ? _array.push(isPass) : ''
				if (!isPass && node.level != 1 && node.parent) {
					this.getReturnNode(node.parent, _array, value)
				}
			},
			//优化之后的代码 不管有几级都可以适用,不过用了递归
			filterNode(value, data, node) {
				console.log(value, data, node)
				if (!value) {
					return true
				}
				let _array = [] //这里使用数组存储 只是为了存储值。
				this.getReturnNode(node, _array, value)
				let result = false
				_array.forEach(item => {
					result = result || item
				})
				return result
			},
			getPaper(row) {
				var that = this;
				console.log(row);
				selFinanceInvoice({
					sku: row.newquotaObj.sku,
					orderNo: row.orderNo
				}).then((res2) => {
					console.log(res2);
					that.paperDict = res2;
					that.paperDict.sku = row.newquotaObj.sku;
					that.paperDialogShow = true;
				})
			},
			findOrgByMangerList() {
				var that = this;
				findOrgByMangerList().then((res) => {
					console.log(11111);
					console.log(res);
					that.orgList = res;
				})
			},
			changePeople(e) {
				console.log(e);
				var that = this;
				for (var i = 0; i < that.peopleList.length; i++) {
					if (e == that.peopleList[i].userId) {
						that.people.account = that.peopleList[i].account;
					}
				}
			},
			exportOrder() {
				var that = this;
				var type = this.searchFormData.type;
				const newSearch = {
					...this.searchFormData,
					...this.pageData,
				}
				console.log(newSearch);
				downloadInvoiceHistory(newSearch).then((res) => {
					let url = window.URL.createObjectURL(new Blob([res], {
						type: "application/vnd.ms-excel"
					}))
					let link = document.createElement('a')
					link.style.display = 'none'
					link.href = url
					link.setAttribute('download', "财务对账.xlsx")
					document.body.appendChild(link)
					link.click()
					that.$message({
						type: 'success',
						message: '导出成功!'
					});
				})
			},
			exportOrder2() {
				var that = this;
				var type = this.searchFormData.type;
				const newSearch = {
					...this.searchFormData,
					...this.pageData,
				}
				console.log(newSearch);
				downloadInvoiceDistribute(newSearch).then((res) => {
					let url = window.URL.createObjectURL(new Blob([res], {
						type: "application/vnd.ms-excel"
					}))
					let link = document.createElement('a')
					link.style.display = 'none'
					link.href = url
					link.setAttribute('download', "分票系统导入模板.xlsx")
					document.body.appendChild(link)
					link.click()
					that.$message({
						type: 'success',
						message: '导出成功!'
					});
				})
			},
			submitHandle(remark) {
				var that = this;
				var arr = this.multipleSelection;
				if (arr.length <= 0) {
					return this.$message({
						type: 'error',
						message: '请选择操作行!'
					});
				}
				var list = [];
				for (var i = 0; i < arr.length; i++) {
					list.push({
						id: arr[i].newquotaObj.id,
						isReceipt: 1,
						remark: remark,
						status: "A1001"
					})
				}
				setReceivingStatus(list).then((res) => {
					console.log(res);
					this.$message({
						type: 'success',
						message: '对账成功!'
					});
					this.getTableData();
					this.remarkDialogShow = false;
				})
			},
			// 检索订单管理列表
			searchControlHandle() {
				// if (this.searchFormData.orgId) {				// 	this.getTableData()				// }else{				// 	this.$message({				// 		type: 'error',				// 		message: '请选择采购组织!'				// 	});				// }
				this.getTableData()
			},
			handleSizeChange(val) {
				this.pageData.pageSize = val
				this.getTableData()
			},
			handleCurrentChange(val) {
				this.pageData.pageNum = val
				this.getTableData()
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
				console.log(val)
			},
			sendHandle() {
				console.log(this.multipleSelection);
				var arr = this.multipleSelection;
				if (arr.length <= 0) {
					return this.$message({
						type: 'error',
						message: '请选择操作行!'
					});
				}
				this.remarkDialogShow = true;
			},
			objectSpanMethod({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				let addIndex = 0;
				if (columnIndex > 0 && columnIndex < 15 || columnIndex > 25) {
					if (rowIndex == row.numAll) {
						return {
							rowspan: row.lengthNum,
							colspan: 1,
						};
					} else {
						return {
							rowspan: 0,
							colspan: 0,
						};
					}
				} else {
					return {
						rowspan: 1,
						colspan: 1,
					};
				}
			},
			// 重置搜索条件
			refreshControlHandle() {
				this.searchFormData = {
					orderNo: "",
					sku: ""
				}
				this.pageData = {
					// 当前分页
					pageNum: 1,
					// 分页条数
					pageSize: 10,
					// 数据总数
					total: 0,
				}
				this.tableData = [];
			},
			// 请求订单管理列表
			getTableData() {
				var type = this.searchFormData.type;
				const newSearch = {
					...this.searchFormData,
					...this.pageData,
				}
				console.log(newSearch);
				selInvoiceHistory(newSearch).then((res) => {
					this.tableData = res.rows;
					this.pageData.total = res.total;
				})

			},
		},
		created() {},
		mounted() {
			// var that = this;
			// window.addEventListener("keyup", this.enterSearch)
			// getOrgChAll().then((res) => {
			// 	that.searchFormData.temp = res.chAll;
			// 	that.getTableData();
			// })
			this.selInvoiceHeader();
			this.getTableData();
		},
	}
</script>

<style>
	.el-button--text {
		display: none;
	}
</style>
